const express = require('express')
const router = express.Router()
const Course = require('../models').Course
const config = require('../config')
const pFormidable = require('../utility/formidable')
const gm = require('gm')

router.post('/upload_img', (req, res, next) => {
  pFormidable(req, config.uploadDir)
    .then(fields => {
      res.json({
        success: true,
        ret: {
          imgSrc: `/api/uploads/${fields.image}`
        }
      })
    })
})

router.get('/crop_img', (req, res, next) => {
  const { height, width, x, y } = req.query
  gm('C:/Users/lpz/Desktop/admin/public/images/ab1.jpg')
    .crop(width, height, x, y)
    .resize(304, 171)
    .write(`C:/Users/lpz/Desktop/ab1-crop-${width}x${height}.jpg`, err => {
      if (err) {
        throw err
      }
      res.json({
        success: true,
        ret: {}
      })
    })
})

module.exports = router
